<?php
class Goods_model extends MY_Model
{
    private $table = 'blues_goods';
    public function __construct()
    {
        parent::__construct();
    }
    public function get_goods_by_id($id)
    {
        $this->db->where("id",$id);
        $query=$this->db->get($this->table);
        return $query->row_array();
    }
    public function get_cat_by_parent_id($parent_id)
    {
        $this->db->where("parent_id",$parent_id);
        $query=$this->db->get($this->table);
        return $query->result_array();
    }
    public function get_goods_list($data, $per_page, $limit, $filds="*")
    {
        $where = " ";
        $limit = " limit {$per_page},{$limit}";
        if(in_array($_SESSION['roleadmin'],[3,4]))
        {
            $where .= " AND operationid={$_SESSION['adminUserInfo']['id']}";
        }

		$sql = "SELECT * FROM blues_goods WHERE isDel=0 {$where}  ORDER BY onlinestatus desc, addtime DESC  {$limit}";
		$query = $this->db->query($sql);
        return $query->result_array();        
    }
    public function get_goods_list_cnt($data)
    {
        $where = " ";
        if(in_array($_SESSION['roleadmin'],[3,4]))
        {
            $where .= " AND operationid={$_SESSION['adminUserInfo']['id']}";
        }

        $sql = "SELECT count(*) cnt FROM blues_goods WHERE isDel=0 {$where} ";
        $query = $this->db->query($sql);
        return $query->row_array()['cnt'];        
    }
    public function add_goods($data)
    {
        $res= $this->db->insert($this->table,$data);
        if($res){
            return $this->db->insert_id();
        }
        return FALSE;
    }
    public function edit_goods($id,$data)
    {
        $this->db->where('id',$id);
        return $this->db->update($this->table,$data);
    }        
    //删除分类
    public function del_goods($goodsid)
    {
		$this->db->where('id',$goodsid);
        return $this->db->update($this->table,array("isDel"=>1));
    }
    /*
     * 获取无限分类ID下面的子类ID集
     *
    */
    public function get_Children_Ids ($cat_id)
    {    
        $ids = "";
		$sql = "SELECT * FROM blues_goods_category WHERE parent_id='{$cat_id}'";
		$query = $this->db->query($sql);
        $rs = $query->result_array ();
        if ($rs){ 
            foreach ($rs as $k=>$row){
                $ids .= ','.$row['cat_id'];
                $ids .= $this->get_Children_Ids ($row['cat_id']);
            }            
        }        
        return $ids;
    }   
    
    /**
     * 获取获取当前分类及所有父分类名字
     *@author KevinBai
     */
    public function get_level_catname_bycid($parent_id){
       $sql = "SELECT * FROM blues_goods_category WHERE cat_id='{$parent_id}'";
       $query = $this->db->query($sql);
       $tem = $query->row_array();
       $pre_name = "";
       if($query->num_rows()){
           $pre_name.= $this->get_level_catname_bycid($tem['parent_id']); 
           $pre_name.= $tem['cat_name'].'->';   
        }
        return $pre_name;
    }  

                  //插入图书图片
    public function add_goods_img($data = array()){
        $res = $this->db->insert("blues_goods_attach",$data);
        return $res;
    }     
    //根据id修改图片
    public function edit_goods_img($id,$data){
        $this->db->where("id",$id);
        $res = $this->db->update("blues_goods_attach",$data);
        return $res;
    }
    public function editExpress($id,$data){
        $this->db->where("id",$id);
        $res = $this->db->update("blues_goods_orders",$data);
        return $res;
    }

    public function editStatus($data = array()){

        $this->db->where("id",$data['id']);
        if($data['type'] == "onlinestatus"){
            $data = array(
                'onlinestatus'=>$data['onlinestatus']
            );
        }
        if($data['type'] == "isDel"){
            $data = array(
                'isDel'=>1
            );
        }

        $res = $this->db->update("blues_goods",$data);
        return $res;
    }

     //获取图片列表
    public function get_img_list_byid($id){
        $this->db->where("goodsid",$id);
        $query = $this->db->get("blues_goods_attach");
        return $query->result_array();
    }
    //根据id删除对应图片
    public function del_attach($id){
        $this->db->where("id",$id);
        $res = $this->db->delete("blues_goods_attach");
        return $res;
    }



    public function getOrderList($data, $per_page, $limit, $filds="*"){
       $where = " ";
        
        
        if(!empty($data['starttime'])){
            $where .= " AND orders.createtime >= {$data['starttime']}";
        }
        if(!empty($data['endtime'])){
            $data['endtime'] = $data['endtime']+3600*24;
            $where .= " AND orders.createtime <= {$data['endtime']}";
        }
        if(!empty($data['keyword'])){
            $where .= " AND orders.goodsName like '%{$data['keyword']}%' OR orders.ordernum like '%{$data['keyword']}%' OR orders.mobile like '%{$data['keyword']}%' OR orders.receiver like '%{$data['keyword']}%'";
        }
        if(!empty($data['status'])){
            $data['status'] = $data['status'];
            $where .= " AND orders.status = {$data['status']}";
        }
        if(in_array($_SESSION['roleadmin'],[3,4]))
        {
            $where .= " AND goods.operationid={$_SESSION['adminUserInfo']['id']}";
        }
        
        $limit = " limit {$per_page},{$limit}";

        $sql = "SELECT {$filds} FROM blues_goods_orders orders,blues_goods goods WHERE orders.goodsID=goods.id and orders.isDel<>1  {$where}  ORDER BY orders.createtime DESC {$limit} ";

        $query = $this->db->query($sql);
        $result =$query->result_array();
        return $result;
    }



    public function getOrderCnt($data){
        $where = " ";
        if(in_array($_SESSION['roleadmin'],[3,4]))
        {
            $where .= " AND goods.operationid={$_SESSION['adminUserInfo']['id']}";
        }
        $sql = "SELECT count(*) cnt FROM blues_goods_orders orders,blues_goods goods WHERE orders.goodsID=goods.id and orders.isDel<>1 {$where}";

        $query = $this->db->query($sql);
        $result =$query->row_array();
        return $result['cnt'];
    }
    public function get_order_by_id($id){
        $this->db->where("id",$id);
        $query=$this->db->get("blues_goods_orders");
        return $query->row_array();
    }

    public function del_order($id){
        $this->db->where("id",$id);
        $res = $this->db->update("blues_goods_orders",array('isDel'=>1));
        return $res;
    }

  

     //编辑状态
    public function setOrderStatus($id,$status){
        $this->db->where("id",$id);
        $data = array(
            'status'=>$status
        );
        $res = $this->db->update("blues_goods_orders",$data);
        return $res;
    }
}

/* End of file flybird/orders_model.php */
/* Location: ./application/models/orders_model.php */